package com.sa.web.behind.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionSupport;
import com.sa.web.bean.NewsBean;
import com.sa.web.dao.service.NewsDao;
import com.sa.web.util.Page;
import com.sa.web.util.Util;

public class NewsManageAction extends ActionSupport {
	
	/** 序列 */
	private static final long serialVersionUID = 1L;
	
	/** 新闻bean*/
	private NewsBean newsBean;
	
	/** 新闻DAO*/
	private NewsDao newsDao;
	
	/** 设置News集合 */
	List<NewsBean> newsList;
	
	/** easyUI中前台数据属性*/
	private JSONArray rows;
	
	/** 信息总数*/
	private JSONArray total;
	
	/** 前台消息对象*/
	private JSONArray msg;

	/** 新闻管理页面初始化页面*/
	public String initNewsFind() throws Exception {
		return SUCCESS;
	}
	
	/** 新闻添加页面初始化页面*/
	public String initNewsAdd() throws Exception {
		return SUCCESS;
	}
	
    /**
     * 新闻分页查询列表
     * @return json
     * @throws Exception
     */
	public String findNewsList()  throws Exception{
		newsList = newsDao.getNewsInfo(Page.getStarIndex(),Page.getPreIndex());
		 rows = JSONArray.fromObject(newsList);
		 total = JSONArray.fromObject(newsDao.getNewsCount());
		 return Util.jSONSuccess();
	}
	
	/**
	 *  增加新闻信息
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String addNewsInfo() throws Exception {
		newsBean.setCreateDate(Util.dateToString(new Date()));
		if(StringUtils.isEmpty(newsBean.getTitle())){
			newsBean.setId(Util.getUUID());
			msg = Util.returnMsg("新闻标题不能为空，请重新输入");
		}
		else if(StringUtils.isEmpty(newsBean.getContent())){
			msg = Util.returnMsg("新闻内容不能为空，请重新输入");			
		}
		else if(newsBean.getContent().length()>500){
			msg = Util.returnMsg("新闻内容不能超过500字，请重新输入");
		}
		else{
		boolean result = newsDao.addNews(newsBean);
		msg = result?Util.returnMsg("添加成功"):Util.returnMsg("添加失败");
		}
		return Util.jSONSuccess();
	}
	
	/**
	 *  删除新闻信息
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String deleteNewsInfo() throws Exception {
		newsDao.deleteNewsInfo(Util.getRequest().getParameter("newsId")==null?"":Util.getRequest().getParameter("newsId").toString());
		msg = JSONArray.fromObject("[\"删除成功\"]");
		return Util.jSONSuccess();
	}

/**************************getter and setter****************************************************************************************************/
    public NewsBean getNewsBean() {
		return newsBean;
	}

	public void setNewsBean(NewsBean newsBean) {
		this.newsBean = newsBean;
	}

	public NewsDao getNewsDao() {
		return newsDao;
	}

	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}

	public List<NewsBean> getNewsList() {
		return newsList;
	}

	public void setNewsList(List<NewsBean> newsList) {
		this.newsList = newsList;
	}

	/**
     * 无get方法无法到页面
     * @return JSONArray
     */
	public JSONArray getRows() {
		return rows;
	}
    /**
     * 无get方法无法到页面
     * @return JSONArray
     */
	public JSONArray getTotal() {
		return total;
	}

	public JSONArray getMsg() {
		return msg;
	}

	public void setMsg(JSONArray msg) {
		this.msg = msg;
	}
	
	
}
